<template>
  <div>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">

          <a-col :md="6" :sm="12">
            <a-form-item label="Banner名称">
              <j-input placeholder="输入名称模糊查询" v-model="queryParam.titleName"></j-input>
            </a-form-item>
          </a-col>
            <a-col :md="6" :sm="8">
              <a-form-item label="描述">
                <a-input placeholder="请输入描述信息" v-model="queryParam.description"></a-input>
              </a-form-item>
            </a-col>
          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQueryInfo" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator" style="border-top: 5px">
      <a-button @click="handleAddInfO" type="primary" icon="plus" >添加Banner</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i>已选择&nbsp;<a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项&nbsp;&nbsp;
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        bordered
        size="middle"
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">
        <template slot="avatarslot" slot-scope="text, record, index">
          <div class="anty-img-wrap">
            <a-avatar shape="square" :src="getAvatarView(record.iconUrl)" icon="user"/>
          </div>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleEditBanner(record)" >编辑</a>
          <a-divider type="vertical" />
            <a @click="handleDeleteBanner(record)" >删除</a>
        </span>
      </a-table>
    </div>
  </a-card>
</div>
</template>

<script>
  import {putAction,getFileAccessHttpUrl} from '@/api/manage';
  import {frozenBatch} from '@/api/api'
  import { addBanner, bannerQueryByid, editBanner,bannerDelete,bannerList } from '@/api/banner'
  import {JeecgListMixin} from '@/mixins/JeecgListMixin'
  import { selectItemsByCode } from '@api/items'

  export default {
    name: "UserList",
    mixins: [JeecgListMixin],
    components: {},
    data() {
      return {
        visible:true,
        description: '这是用户管理页面',
        queryParam: {},
        recycleBinVisible: false,
        columns: [
          {
            title: 'Banner名称',
            align: "center",
            dataIndex: 'titleName',
            width: 120,
            sorter: true
          },
          {
            title: 'Banner',
            align: "center",
            width: 120,
            dataIndex: 'iconUrl',
            scopedSlots: {customRender: "avatarslot"}
          },

          {
            title: '描述',
            align: "center",
            width: 80,
            dataIndex: 'description',
            sorter: true
          },
          {
            title: '备注',
            align: "center",
            width: 100,
            dataIndex: 'memo'
          },
          {
            title: '状态',
            align: "center",
            width: 100,
            dataIndex: 'status_dictText'
          },
          {
            title: '创建人',
            align: "center",
            width: 180,
            dataIndex: 'createBy'
          },
          {
            title: '创建时间',
            align: "center",
            width: 180,
            dataIndex: 'createTime'
          },
          {
            title: '操作',
            dataIndex: 'action',
            scopedSlots: {customRender: 'action'},
            align: "center",
            width: 170
          }
        ],
        url: {
          syncUser: "/act/process/extActProcess/doSyncUser",
          list: "/sys/website/sysImage/list",
          delete: "/sys/website/sysImage/delete",
          deleteBatch: "/sys/website/sysImage/deleteBatch",
          exportXlsUrl: "/sys/website/sysImage/exportXls",
        },
      }
    },
    computed: {},
    methods: {
      searchQueryInfo(){
        bannerList(this.queryParam).then((res)=>{
              if(res.success){
                this.dataSource = res.result.records;
                this.ipagination.total = res.result.total;
              }
        })
      },
      handleEditBanner: function(record){
        console.log(JSON.stringify(record));
        this.$router.push({path:'/website/banner/add',query:{id:record.id}});
      },
      handleDeleteBanner:  function(record){
        let  that  = this;
        if(record.id) {
          let demo  ={ id:record.id }
          bannerDelete(demo).then(res=>{
              if(res.code == 200){
                that.$message.success('删除成功');
              }
          })
          that.loadData();
         }
      },
      handleAddInfO: function () {
        this.$router.push({path:'/website/banner/add'});
      },
      getAvatarView: function (avatar) {
        return getFileAccessHttpUrl(avatar)
      },
      batchFrozen: function (status) {
        if (this.selectedRowKeys.length <= 0) {
          this.$message.warning('请选择一条记录！');
          return false;
        }else{
          let ids = "";
          let that = this;
          let isAdmin = false;
          that.selectionRows.forEach(function (row) {
            if (row.username == 'admin') {
              isAdmin = true;
            }
          });
          if (isAdmin) {
            that.$message.warning('管理员账号不允许此操作,请重新选择！');
            return;
          }
          that.selectedRowKeys.forEach(function (val) {
            ids += val + ",";
          });
          that.$confirm({
            title: "确认操作",
            content: "是否" + (status == 1 ? "解冻" : "冻结") + "选中账号?",
            onOk: function () {
              frozenBatch({ids: ids, status: status}).then((res) => {
                if (res.success) {
                  that.$message.success(res.message);
                  that.loadData();
                  that.onClearSelected();
                } else {
                  that.$message.warning(res.message);
                }
              });
            }
          });
        }
      },
      handleMenuClick(e) {
        if (e.key == 1) {
          this.batchDel();
        } else if (e.key == 2) {
          this.batchFrozen(2);
        } else if (e.key == 3) {
          this.batchFrozen(1);
        }
      },
      handleFrozen: function (id, status, username) {
        let that = this;
        //TODO 后台校验管理员角色
        if ('admin' == username) {
          that.$message.warning('管理员账号不允许此操作！');
          return;
        }
        frozenBatch({ids: id, status: status}).then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            that.loadData();
          } else {
            that.$message.warning(res.message);
          }
        });
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>